package view.room;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import constants.RoomConstants;
import entity.Room;


public class RoomPanel extends JPanel{
	String[] typeNames   = RoomConstants.TYPE_NAME;
	String[] statusNames = RoomConstants.STATUS_NAME;
	
	int typeFilter   = RoomConstants.ALL_TYPE;
	int statusFilter = RoomConstants.ALL_STATUS;
	
	String[] adminStuff = new String[]{
			"Thêm phòng", "Sửa phòng", "Xóa phòng", "Báo hỏng", "Thiết lập bình thường"
	};
	JButton[] btStuff = new JButton[adminStuff.length];
	
	RoomContent roomContent;
	
	public RoomPanel(){
		setLayout(new BorderLayout());		
		
		add(new StuffButton(), BorderLayout.NORTH);
		add(roomContent= new RoomContent());
		add(new SideButton(), BorderLayout.EAST);
	}
	
	public class SideButton extends JPanel{
		
		public SideButton(){
			setLayout(new FlowLayout(FlowLayout.LEFT));
			setPreferredSize(new Dimension(120, 20));
			
			JLabel lbType = new JLabel("Type Filter");
			lbType.setPreferredSize(new Dimension(120, 30));
			add(lbType);
			
			for (int i=0; i<typeNames.length; i++){
				JButton btType = new JButton(typeNames[i]);
				btType.setPreferredSize(new Dimension(120,30));
				btType.addActionListener(new TypeFilterListener(i));
				add(btType);
			}
			
			JLabel lbStatus = new JLabel("Status Filter");
			lbStatus.setPreferredSize(new Dimension(120, 30));
			add(lbStatus);
			
			for (int i=0; i<statusNames.length; i++){
				JButton btStatus = new JButton(statusNames[i]);
				btStatus.setPreferredSize(new Dimension(120,30));
				btStatus.addActionListener(new StatusFilterListener(i));
				add(btStatus);
			}
		}
	}
	
	class StuffButton extends JPanel{
		public StuffButton(){
			setLayout(new FlowLayout(FlowLayout.LEFT));
			setPreferredSize(new Dimension(0, 50));
			for (int i=0; i<btStuff.length; i++){
				btStuff[i] = new JButton(adminStuff[i]);
				btStuff[i].addActionListener(new AdminStuffListener());
				btStuff[i].setPreferredSize(new Dimension(140, 30));
				add(btStuff[i]);
			}
		}
	}
	
	public void addRoom(){
		Room room = new RoomOperate((JFrame)SwingUtilities.getRoot(this), 
						"Thêm phòng",
						new Room()).showAndReturn();
	}
	public void editRoom(){
		Room room = new RoomDialog((JFrame)SwingUtilities.getRoot(this), 
				"Sửa phòng",
				new Room()).showAndReturn();
	}
	public void deleteRoom(){
		int result = JOptionPane.showOptionDialog(SwingUtilities.getRoot(this),
				"Xóa phòng?", "Xóa phòng", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
				null, new String[]{"Xóa", "Hủy bỏ"}, 0);
		switch(result){
		case 0:
			break;
		case 1:
			break;
		case -1:
			break;
		}
	}
	public void notifyCorrupt(){
		
	}
	public void setNormal(){
		
	}
	class TypeFilterListener implements ActionListener{
		private int index;
		public TypeFilterListener(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent event){
			typeFilter = index;
			roomContent.filter(typeFilter, statusFilter);
		}
	}
	
	class StatusFilterListener implements ActionListener{
		private int index;
		public StatusFilterListener(int index){
			this.index = index;
		}
		public void actionPerformed(ActionEvent event){
			statusFilter = index;
			roomContent.filter(typeFilter, statusFilter);
		}
	}
	
	class AdminStuffListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			if (event.getSource() == btStuff[0]){
				addRoom();
			}else if (event.getSource() == btStuff[1]){
				editRoom();
			}else if (event.getSource() == btStuff[2]){
				deleteRoom();
			}else if (event.getSource() == btStuff[3]){
				notifyCorrupt();
			}else if (event.getSource() == btStuff[4]){
				setNormal();
			}
		}
	}
}
